// physical-html
//
// Copyright (c)Daigo Tanaka
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

physHtml.particle = function(id)
{
this.id = id;
this.position = $V([0.0, 0.0]);
this.offset = $V([-0.5, -0.5]);
this.rotationOffset = $V([0, 0]);
this.width = 0.0;
this.height = 0.0;
this.radius = 0.0;
this.orientation = 0.0;
this.lastUpdatedTime = 0.0;
this.delta = 0.0;
this.newPosition = $V([0.0, 0.0]);
this.velocity = $V([0.0, 0.0]);
this.acceleration = $V([0.0, 0.0]);
this.force = $V([0.0, 0.0]);
this.mass = 1.0;
this.gravity = $V([0.0, 0.0]);
this.drag = 0.001;

this.connectors = new Array();
this.connect = function(toObject, forceSource)
{
  var connector = new physHtml.connector(this, toObject, forceSource);
  this.connectors.push(connector);
}

this.solve = function(time)
{
//  PhysicalHtmlDebugMessage = "time=" + time + " ";
  this.delta = (time - this.lastUpdatedTime) / 1000.0;
  if (this.delta == 0.0 || this.mass == 0.0){
    return;
  }
  this.solveForce(time);
  this.solveAcceleration();
  this.solveVelocity();
  this.solvePosition();
  this.lastUpdatedTime = time;
}

this.update = function()
{
  this.position = this.newPosition;
}

this.solvePosition = function()
{
  deltaPos = this.velocity.x( this.delta );
  this.newPosition = this.position.add(deltaPos);
}

this.solveVelocity = function()
{
  this.velocity = this.velocity.add( this.acceleration.x(this.delta) );
  for (var i = 0; i < this.connectors.length; i++){
    this.velocity = this.connectors[i].velocity(this.position, this.radius, this.velocity);
  }
  
  if (this.velocity.distanceFrom(Vector.Zero(2)) < physHtml.VelocityCutOff){
    this.velocity = Vector.Zero(2);
  }
}

this.solveAcceleration = function()
{
  this.acceleration = ( this.force.x( 1.0 / this.mass ).add( this.gravity ) );
  if (this.acceleration.distanceFrom(Vector.Zero(2)) < physHtml.AccelerationCutOff){
    this.acceleration = Vector.Zero(2);
  }
}

this.solveForce = function()
{
  this.force = $V([0.0, 0.0]);
  for (var i = 0; i < this.connectors.length; i++){
    var currentForce = this.connectors[i].force(this.position, this.radius, this.velocity);
    var forceMag = currentForce.distanceFrom(Vector.Zero(2));
    this.force = $V([this.force.e(physHtml.Index0) + currentForce.e(physHtml.Index0) - this.velocity.e(physHtml.Index0) * this.velocity.e(physHtml.Index0) * this.drag,
                     this.force.e(physHtml.Index1) + currentForce.e(physHtml.Index1) - this.velocity.e(physHtml.Index1) * this.velocity.e(physHtml.Index1) * this.drag ]);     
  }
} 
}

